package main.social.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import main.social.dao.EntryGroupDao;
import main.social.entities.Entrygroup;

public class EntryGroupDaoImpl extends AbstractGenericDao<Entrygroup, Long>
		implements EntryGroupDao {

	private static String DEFAULT_GROUP = "DEFAULT";

	@SuppressWarnings("rawtypes")
	@Override
	public Entrygroup getDefaultGroup() {
		Entrygroup result = null;
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				try {
					Criteria crit = session
							.createCriteria(getPersistentClass());
					crit.add(Restrictions.eq("name", DEFAULT_GROUP));
					return crit.list();
				} catch (HibernateException he) {
					return null;
				}
			}
		});
		if ((list != null) && (list.size() > 0)) {
			result = (Entrygroup) list.get(0);
		}
		return result;
	}

}
